/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2010-10-8
 * Time: 16:10:44
 * @author vhly[FR]
 */
package com.vhly.openmail.apiservice;

import java.math.BigInteger;
import java.security.*;
import java.util.Hashtable;

public final class APIConsumerManager {
    private static APIConsumerManager outInstance;

    private static KeyPairGenerator kpg;
    private static MessageDigest sha1;

    public static APIConsumerManager getInstance() {
        if (outInstance == null) {
            outInstance = new APIConsumerManager();
        }
        return outInstance;
    }

    private Hashtable<String, APIConsumer> consumers;
    private Hashtable<String, String> names;

    private APIConsumerManager() {
        consumers = new Hashtable<String, APIConsumer>();
        names = new Hashtable<String, String>();
    }

    public APIConsumer getConsumer(String apiKey) {
        APIConsumer ret = null;
        if (apiKey != null) {
            ret = consumers.get(apiKey);
        }
        return ret;
    }

    public APIConsumer getConsumerByName(String name) {
        APIConsumer ret = null;
        if (name != null) {
            String apiKey = names.get(name);
            if(apiKey != null){
                ret = consumers.get(apiKey);
            }
        }
        return ret;
    }

    public boolean addConsumer(APIConsumer consumer) {
        boolean bret = false;
        if (consumer != null) {
            String k = consumer.apiKey;
            String n = consumer.apiConsumerName;
            if (!consumers.containsKey(k)) {
                consumers.put(k, consumer);
                names.put(n,k);
                bret = true;
            }
        }
        return bret;
    }

    public boolean updateConsumer(APIConsumer consumer) {
        boolean bret = false;
        if (consumer != null) {
            String k = consumer.apiKey;
            if (consumers.containsKey(k)) {
                consumers.put(k, consumer);
                bret = true;
            }
        }
        return bret;
    }

    public static APIConsumer registerNewConsumer() {
        APIConsumer ret = null;
        if (kpg == null) {
            try {
                kpg = KeyPairGenerator.getInstance("RSA");
                kpg.initialize(512, new SecureRandom());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        if (kpg != null) {
            KeyPair pair = kpg.genKeyPair();
            PublicKey publicKey = pair.getPublic();
            PrivateKey privateKey = pair.getPrivate();
            byte[] buf1 = publicKey.getEncoded();
            byte[] buf2 = privateKey.getEncoded();
            if (sha1 == null) {
                try {
                    sha1 = MessageDigest.getInstance("SHA-1");
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }
            if (sha1 != null) {
                sha1.reset();
                byte[] digest = sha1.digest(buf1);
                BigInteger big = new BigInteger(digest).abs();
                ret = new APIConsumer();
                ret.apiKey = big.toString(36).toUpperCase();
                big = null;
                sha1.reset();
                digest = sha1.digest(buf2);
                big = new BigInteger(digest).abs();
                ret.apiSecret = big.toString(36).toUpperCase();
                ret.createTime = System.currentTimeMillis();
                big = null;
            }
        }
        return ret;
    }
}
